<template>
  <div>
    <div class="login-form">
      <el-input v-model="inputname" placeholder="输入姓名"></el-input>
      <el-input v-model="inputnum" placeholder="输入学号"></el-input>
      <el-button type="primary" @click="userman()">登陆</el-button>
    </div>
  </div>
</template>
<style scoped>
.login-form {
  margin: 20% auto;
  width: 30%;
}
.el-input {
  margin-bottom: 30px;
}
.el-button {
  width: 3rem;
  font-weight: 500;
  font-size: 20px;
  /* 文字间隔 */
}
</style>
<script>
import { del, get } from "@/utils/request";
import axios from "axios";
export default {
  data() {
    return {
      application: [],
      inputname: "",
      inputnum: "",
      userid: "",
      username: "",
      usernumber:"",
    };
  },
  methods: {
    async userman() {
      let url = "http://120.77.41.127:8080/nameUser/query";
      let data = await get(url);
      this.application = data.data;
      let flag = 0;
      //1.遍历数组
      for (let i = 0; i < this.application.length; i++) {
        let item = this.application[i];
        if (item.number == this.inputnum) {
          //2.登陆成功，跳转页面
          flag = 1;
          this.userid = item.id;
          this.username = item.name;
          this.usernumber = item.number;
          sessionStorage.setItem("userId", this.userid);
          sessionStorage.setItem("userName", this.username);
          sessionStorage.setItem("userNumber", this.usernumber);
          this.$message({
            message: "登陆成功!",
            type: "success"
          });
          this.$router.replace({
            path: "/information"
          });
          console.log(flag);
          break;
        }
      }
      //3.未找到该用户，用户不存在
      if (flag == 0) {
        this.$message.error("用户不存在，请先注册！");
        this.$router.replace({
          path: "/register"
        });
      }
    }
  }
};
</script>